package com.gopay.common.gateway.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.pg.PgGenOrderTransDtl;
import com.gopay.common.gateway.dao.PgGeneralOrderTransDtlQueryDAO;

/**
 * 网关通用订单交易DAO
 * 
 * @PgGeneralOrderTransDtlQueryDAOImpl.java
 * @author GongZhong
 * @2012-12-28 下午01:46:54 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("pgGeneralOrderTransDtlQueryDAO")
public class PgGeneralOrderTransDtlQueryDAOImpl extends BaseDAOImpl<PgGenOrderTransDtl, Serializable> implements
        PgGeneralOrderTransDtlQueryDAO {

    @Override
    public List<PgGenOrderTransDtl> findPgDtls(String merId, String merOrderId){
        String hql = " from PgGenOrderTransDtl t where t.merId = :merId and t.merOrderId = :merOrderId order by t.transDtlSq asc ";
        Map param = new HashMap<String,Object>();
        param.put("merId", merId);
        param.put("merOrderId", merOrderId);
        return findByHql(hql, param);
    }

    @Override
    public boolean checkPgTranDtlExists(String merId, String merOrderId, int transDtlSq){
        StringBuilder sb =new StringBuilder();
        sb.append(" select t.trans_dtl_sq  from pg_gen_order_trans_dtl t where 1=1 ");
        sb.append(" and t.trans_dtl_sq!=:tranDtlSq ");
        sb.append(" and t.mer_order_id=:merOrderId ");
        sb.append(" and t.mer_id=:merId ");

        Map param = new HashMap<String,Object>();
        param.put("merId", merId);
        param.put("merOrderId", merOrderId);
        param.put("tranDtlSq", transDtlSq);

        int count=findCountBySql(sb.toString(), param);
        return count>0;
    }

    @Override
    public List<PgGenOrderTransDtl> findPgOrderTransDtl(int transDtlSq) {
        String hql = " from PgGenOrderTransDtl t where t.transDtlSq = :transDtlSq";
        Map param = new HashMap<String,Object>();
        param.put("transDtlSq", transDtlSq);
        return findByHql(hql, param);
    }
}
